package com.example.todo;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<String> data;
    private int position;
    final NoteOperator noteOperator = new NoteOperator(context);
    private DBOpenHelper DBOpenHelper=new DBOpenHelper(context);

    public void NoteOperator(Context context) {
        DBOpenHelper = new DBOpenHelper(context);
    }


    public MyAdapter(List<String> data){
        this.data = data;
    }
    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }
    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(context == null)
            context = viewGroup.getContext();
        if(view == null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,null);
            viewHolder = new ViewHolder();
            viewHolder.mTv = (TextView)view.findViewById(R.id.note_title);
            viewHolder.mBtn = (Button)view.findViewById(R.id.btnok);
            view.setTag(viewHolder);
        }
        viewHolder = (ViewHolder)view.getTag();
        //设置tag标记
        viewHolder.mBtn.setTag(R.id.btnok,i);//添加此代码
//        viewHolder.mBtn.setText("点我点我"+ i);
        viewHolder.mBtn.setOnClickListener(this);
        viewHolder.mTv.setText(data.get(i));
        //设置tag标记
        viewHolder.mTv.setTag(R.id.note_title,i);//添加此代码

        return view;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnok:
                ViewHolder viewHolder = null;
                int b = (int) view.getTag(R.id.btnok);
//                Toast.makeText(context,"我是按钮 " + b,Toast.LENGTH_SHORT).show();

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("确定完成并删除该待办吗？");
                builder.setTitle("提示");

                //添加AlterDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        NoteOperator noteOperator = new NoteOperator(context);
                        noteOperator.btn_delete(data.get(b));

                        Intent intent = new Intent(context, MainActivity.class);
                        context.startActivity(intent);

                    }
                });

                //添加AlterDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.create().show();
                break;
        }
    }
    static class ViewHolder{
        TextView mTv;
        Button mBtn;
    }
}